/**
 * SSP for web
 * Copyright 2014 Baidu Inc. All rights reserved.
 *
 * @file `FormViewPanel`控件，伪装成输入控件，有以下三个作用:
 *
 * - 触发子视图中的控件校验
 * - 获取、设置子视图中输入控件的`value`
 * - 显示后端或model层校验错误信息
 *
 * @exports ui.FormViewPanel
 * @author yanghuabei(yanghuabei@baidu.com)
 */
define(
    function (require) {
        var Promise = require('promise');
        var ui = require('esui/main');

        /**
         * `FormViewPanel`控件，伪装成输入控件
         *
         * @class ui.FormViewPanel
         * @extends ui.ChildViewPanel
         */
        var exports = {};

        /**
         * 控件类型，始终为`"FormViewPanel"`
         *
         * @type {string}
         * @readonly
         * @override
         */
        exports.type = 'FormViewPanel';

        /**
         * 获取控件的分类，默认返回`"input"`以表示为输入控件
         *
         * @method FormViewPanel.prototype.getCategory
         * @return {string}
         * @override
         */
        exports.getCategory = function () {
            return 'input';
        };

        /**
         * 获取子视图中输入控件的`rawValue`，子视图提供的`getViewData`方法，
         *
         * @method FormViewPanel.prototype.getRawValue
         * @return {Object | null}
         */
        exports.getRawValue = function () {
            var rawValue = null;
            var view = this.view;
            // 如果子视图提供了`getViewData`方法，调用并返回
            if (typeof view.getViewData === 'function') {
                rawValue = view.getViewData();
            }

            return rawValue;
        };

        /**
         * 调用子视图的setViewData方法
         *
         * @method FormViewPanel.prototype.setRawValue
         * @param {Object} rawValue 值对象
         * @return {Promise} 成功后返回子视图对象
         */
        exports.setRawValue = function (rawValue) {
            var setDataToView = function (view) {
                if (typeof view.setViewData === 'function') {
                    view.setViewData(rawValue);
                }
                return view;
            };

            // 如果子视图未完成加载，等加载完成后再`setRawValue`
            return Promise.resolve(this.view).then(setDataToView);
        };

        /**
         * 调用子视图的`setReadOnly`
         *
         * @method FormViewPanel.prototype.setReadOnly
         * @param {boolean} readOnly 状态标志
         * @return {Promise} 成功后返回子视图对象
         */
        exports.setReadOnly = function (readOnly) {
            var setReadOnly = function (view) {
                if (typeof view.setReadOnly === 'function') {
                    view.setReadOnly(readOnly);
                }
                return view;
            };

            return Promise.resolve(this.view).then(setReadOnly);
        };

        /**
         * 调用子视图的`disableInputControls`
         *
         * @method FormViewPanel.prototype.disableInputControls
         * @return {Promise} 成功后返回子视图对象
         */
        exports.disableInputControls = function () {
            var disable = function (view) {
                if (typeof view.disableInputControls === 'function') {
                    view.disableInputControls();
                }

                return view;
            };

            return Promise.resolve(this.view).then(disable);
        };

        /**
         * 调用子视图的`enableInputControls`
         *
         * @method FormViewPanel.prototype.enableInputControls
         * @return {Promise} 成功后返回子视图对象
         */
        exports.enableInputControls = function () {
            var enable = function (view) {
                if (typeof view.enableInputControls === 'function') {
                    view.enableInputControls();
                }
                return view;
            };

            return Promise.resolve(this.view).then(enable);
        };

        /**
         * 调用子视图提供的显示错误信息的方法通知用户提交错误
         *
         * @method FormViewPanel.prototype.notifyErrors
         * @param {Object[]} errors 错误信息
         * @return {Promise} 成功后返回子视图对象
         */
        exports.notifyErrors = function (errors) {
            var notify = function (view) {
                if (typeof view.notifyErrors === 'function') {
                    view.notifyErrors(errors);
                }
                return view;
            };

            return Promise.resolve(this.view).then(notify);
        };

        /**
         * 验证子视图中输入控件，同时会触发输入控件的错误信息提示逻辑，
         * 所有状态为禁用的输入控件、`name`属性为空的控件不参与检验
         *
         * @return {boolean} 返回`true`表示验证通过
         */
        exports.validate = function () {
            if (typeof this.view.validate === 'function') {
                return this.view.validate();
            }

            return true;
        };

        var ChildViewPanel = require('ui/ChildViewPanel');
        var FormViewPanel = require('eoo').create(ChildViewPanel, exports);

        ui.register(FormViewPanel);

        return FormViewPanel;
    }
);
